package com.wuxb.masterWorker;

import java.util.Random;
import java.util.concurrent.ConcurrentSkipListSet;

public class Main {

    public static void main(String[] args) {
        System.out.println(Runtime.getRuntime().availableProcessors());
        Master master = new Master<Task>(new MyWorker(), Runtime.getRuntime().availableProcessors());
        Random random = new Random();
        for (int i = 0; i < 99; i++) {
            Task task = new Task();
            task.setId(i);
            task.setName("task"+i);
            task.setPrice(random.nextInt(1000));
            master.submit(task);
        }

        master.execute();

        long startTime = System.currentTimeMillis();
        while (true){
            if (master.isComplete()){
                long end = System.currentTimeMillis() - startTime;
                ConcurrentSkipListSet resultMap = master.getResultMap();
                int result = 0;
                for (Object o : resultMap) {
                    result+=(Integer) o;
                }
                System.out.println("计算结果为"+result+" ,所花费时间为"+end);
                break;
            }
        }
    }
}
